package com.fish.anne.webapp.action.front;

import java.util.Date;
import java.util.List;

import com.fish.anne.model.Auto;
import com.fish.anne.model.AutoBrand;
import com.fish.anne.model.SystemRegion;
import com.fish.anne.service.AutoBrandManager;
import com.fish.anne.service.AutoManager;
import com.fish.anne.service.SystemRegionManager;
import com.fish.anne.webapp.action.BaseAction;
import com.fish.anne.webapp.formbean.FrontAutoSearch;
import com.fish.anne.webapp.util.DateTimeFormatUtil;
import com.opensymphony.xwork2.Preparable;

public class AutoAction extends BaseAction implements Preparable {
    private AutoManager autoManager;
    private List<Auto> autoes;
    private Auto auto;
    private Long  autoId;

    private SystemRegionManager systemRegionManager;
    
    private List<SystemRegion> systemRegions ;
    
    private SystemRegion systemRegion;
    private FrontAutoSearch frontAutoSearch;
    
    

	private AutoBrandManager autoBrandManager;

	private List<AutoBrand> autoBrands;

	public List<AutoBrand> getAutoBrands() {
		return autoBrands;
	}

	public void setAutoBrands(List<AutoBrand> autoBrands) {
		this.autoBrands = autoBrands;
	}

	public void setAutoBrandManager(AutoBrandManager autoBrandManager) {
		this.autoBrandManager = autoBrandManager;
	}
    
    public FrontAutoSearch getFrontAutoSearch() {
		return frontAutoSearch;
	}

	public void setFrontAutoSearch(FrontAutoSearch frontAutoSearch) {
		this.frontAutoSearch = frontAutoSearch;
	}

	public SystemRegion getSystemRegion() {
		return systemRegion;
	}

	public void setSystemRegion(SystemRegion systemRegion) {
		this.systemRegion = systemRegion;
	}

	public List<SystemRegion> getSystemRegions() {
		return systemRegions;
	}

	public void setSystemRegions(List<SystemRegion> systemRegions) {
		this.systemRegions = systemRegions;
	}

	public void setSystemRegionManager(SystemRegionManager systemRegionManager) {
		this.systemRegionManager = systemRegionManager;
	}

	public void setAutoManager(AutoManager autoManager) {
        this.autoManager = autoManager;
    }

    public List<Auto> getAutoes() {
        return autoes;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String autoId = getRequest().getParameter("auto.autoId");
            if (autoId != null && !autoId.equals("")) {
                auto = autoManager.get(new Long(autoId));
            }
        }
    }

    public String list() throws Exception {
    	if(frontAutoSearch == null || frontAutoSearch.getBeginDate() == null ||"".equals(frontAutoSearch.getBeginDate().trim())){
    		if(frontAutoSearch==null){
    			frontAutoSearch = new FrontAutoSearch();
    		}
    		Date day = new Date();
    		Date other = DateTimeFormatUtil.addTimeByDays(day,3);
    		String today = DateTimeFormatUtil.convertDateToString(day, DateTimeFormatUtil.YEAR_MONTH_DAY_TEMPLATE_1);
    		String _other = DateTimeFormatUtil.convertDateToString(other, DateTimeFormatUtil.YEAR_MONTH_DAY_TEMPLATE_1);
    		frontAutoSearch.setBeginDate(today);
    		frontAutoSearch.setEndDate(_other);
    	}
    	if( frontAutoSearch.getEndDate() == null ||"".equals(frontAutoSearch.getEndDate().trim())){
    		Date day = DateTimeFormatUtil.convertStringToDate(frontAutoSearch.getBeginDate(), DateTimeFormatUtil.YEAR_MONTH_DAY_TEMPLATE_1);
    		Date other = DateTimeFormatUtil.addTimeByDays(day,3);
    		String _other = DateTimeFormatUtil.convertDateToString(other, DateTimeFormatUtil.YEAR_MONTH_DAY_TEMPLATE_1);    		
    		frontAutoSearch.setEndDate(_other);
    	}
        long totalcount = autoManager.getCountByMember(frontAutoSearch);
        frontAutoSearch.setPageTotalRow(totalcount);
        if(getPageCurrentPage()!=null){
        	frontAutoSearch.setPageCurrentPage(getPageCurrentPage());
        }        
        autoes = autoManager.findByMember(frontAutoSearch);
        this.systemRegions = this.systemRegionManager.getAll();
        this.autoBrands  = this.autoBrandManager.getAll();
        return SUCCESS;
    }

    public void setAutoId(Long  autoId) {
        this. autoId =  autoId;
    }

    public Auto getAuto() {
        return auto;
    }

    public void setAuto(Auto auto) {
        this.auto = auto;
    }

 

    public String edit() {
    	boolean ok = false;
    	try {
			ok = DateTimeFormatUtil.compareDateByString(this.frontAutoSearch.getBeginDate(), frontAutoSearch.getEndDate() ,DateTimeFormatUtil.YEAR_MONTH_DAY_TEMPLATE_1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
        if (ok & autoId != null) {
            auto = autoManager.get(autoId);
            this.systemRegions = this.systemRegionManager.getAll();
        } else{
        	return INPUT;
        }

        return SUCCESS;
    }

 
    public String save() throws Exception {


        boolean isNew = (auto.getAutoId() == null);

        autoManager.save(auto);

        String key = (isNew) ? "auto.added" : "auto.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}